package com.example.hxd.zndzg.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.hxd.zndzg.R;
import com.example.hxd.zndzg.adapter.CardPagerAdapter;
import com.example.hxd.zndzg.adapter.ShadowTransformer;
import com.example.hxd.zndzg.entity.CardItem;
import com.example.hxd.zndzg.util.ToastUtils;
import com.example.hxd.zndzg.view.NoScrollViewPager;
import com.example.hxd.zndzg.view.SatelliteMenu;

public class BoxsFragmeng extends Fragment {
    private View view;
    private Context mContext;
    private Activity mActivity;
    private ImageView boxOpen;
    private ImageView boxClose;
    private ImageView boxSearcd;
    private ImageView boxAdd;
    private ImageView boxUpdata;

    private NoScrollViewPager mViewPager;

    private CardPagerAdapter mCardAdapter;
    private ShadowTransformer mCardShadowTransformer;

    private SatelliteMenu satelliteMenu;


    @SuppressLint("ValidFragment")
    public BoxsFragmeng(Context mContext, Activity activity) {
        this.mContext = mContext;
        mActivity = activity;
    }

    public BoxsFragmeng() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_boxs, null);
        initView();
        return view;
    }

    private void initView() {
        ToastUtils.init(mContext);
        mViewPager = view.findViewById(R.id.viewPager);
        satelliteMenu = view.findViewById(R.id.satelliteMenu);
        mCardAdapter = new CardPagerAdapter();
        mCardAdapter.addCardItem(new CardItem(R.string.title_1, R.string.text_1), mContext, mActivity);
        mCardAdapter.addCardItem(new CardItem(R.string.title_2, R.string.text_1), mContext, mActivity);
        mCardAdapter.addCardItem(new CardItem(R.string.title_3, R.string.text_1), mContext, mActivity);
        mCardAdapter.addCardItem(new CardItem(R.string.title_4, R.string.text_1), mContext, mActivity);
        mCardShadowTransformer = new ShadowTransformer(mViewPager, mCardAdapter);
        mCardShadowTransformer.enableScaling(true);
        mViewPager.setAdapter(mCardAdapter);
        mViewPager.setPageTransformer(false, mCardShadowTransformer);
        mViewPager.setOffscreenPageLimit(3);

        mCardAdapter.setOnCircleMenuClickListener(new CardPagerAdapter.CircleMenuListener() {
            @Override
            public void onCircleMenuClick(final int item, final int position) {
                satelliteMenu.setVisibility(View.VISIBLE);
                mViewPager.setNoScroll(true);
                mCardAdapter.setButtonClickable(true);
                satelliteMenu.setOnCircleMenuClickListener(new SatelliteMenu.OnSatelliteMenuItemClickListener() {
                    @Override
                    public void onClick(View view, int pos) {
                        switch (pos){
                            case 1:
                                OpenBox(item, position);
                                break;

                            case 2:
                                ClosseBox(item, position);
                                break;

                            case 3:
                                SearchBox(item, position);
                                break;

                            case 4:
                                AddObjBox(item, position);
                                break;

                            case 5:
                                UpdataBox(item, position);
                                break;
                        }
                        mCardAdapter.setButtonClickable(false);
                        mCardAdapter.setBackgroundColor(item, position);
                        mViewPager.setNoScroll(false);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                satelliteMenu.setVisibility(View.GONE);
                            }
                        }, 600);
                    }
                });
            }
        });
    }

    /**
     * 修改抽屉内物体的信息
     */
    private void UpdataBox(int item, int position) {
        ToastUtils.show("您修改了" + (item + 1) + "号柜中的第" + position + "个抽屉中的物体信息");
    }

    /**
     * 往抽屉里添加东西，添加描述信息
     */
    private void AddObjBox(int item, int position) {
        ToastUtils.show("您往" + (item + 1) + "号柜中的第" + position + "个抽屉中添加了物体信息");
    }

    /**
     * 查看抽屉内的信息
     */
    private void SearchBox(int item, int position) {
        ToastUtils.show("您查看了了" + (item + 1) + "号柜中的第" + position + "个抽屉");
    }

    /**
     * 关闭抽屉方法
     */
    private void ClosseBox(int item, int position) {
        ToastUtils.show("您关闭了" + (item + 1) + "号柜中的第" + position + "个抽屉");
    }

    /**
     * 打开抽屉方法
     */
    private void OpenBox(int item, int position) {
        ToastUtils.show("您打开了" + (item + 1) + "号柜中的第" + position + "个抽屉");
    }

}
